﻿using System;
using System.Data;
using System.Linq;
using DAL;
using Entity;
using NLog;

public partial class mobile_Articles : System.Web.UI.Page
{
    #region Variables
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    Articls objArticls;
    #endregion

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                GetMainArticlsNews(); 
            }
        }
        catch (Exception ex)
        {
            _logger.Error("mobile_Articles:::Mobile Version::::Page_Load:::" + ex.Message);
        }
    }
    #endregion

    #region Methods

    /// <summary>
    /// Get Main Articls By Articles ID
    /// </summary>
    private void GetMainArticlsNews()
    {
        try
        {
            if (Request.Url.AbsoluteUri.IndexOf("ArticlesID=") >= 0)
            {
                var numbersFromString = new String(Request.QueryString["ArticlesID"].ToString().Where(x => x >= '0' && x <= '9').ToArray());
                hfArticleID.Value = numbersFromString;
                ManageArticls objManageArticls = new ManageArticls();
                objArticls = objManageArticls.GetArticlsInfo(hfArticleID.Value);
                hfCategoryID.Value = objArticls.CategoryID.ToString();
                PageTitle.Text = objArticls.ArticlsTitle + " | LiveKora لايف كورة ";

                if (objArticls.ArticlsSmallDescription.Length > 27)
                    Page.MetaDescription = objArticls.ArticlsSmallDescription.Remove(0, 27);
                else
                    Page.MetaDescription = objArticls.ArticlsSmallDescription;

                objArticls.ArticlsCounter = objArticls.ArticlsCounter + 1;
                objArticls.ArticlsID = Convert.ToInt32(hfArticleID.Value);
                objManageArticls.EditArticlsCounter(objArticls.ArticlsID, objArticls.ArticlsCounter);
                DataSet objDataSet = objManageArticls.GetArticlsDataByArticlsID(objArticls.ArticlsID);

                rptArticles.DataSource = objDataSet;
                rptArticles.DataBind();

                rptManageCategory.DataSource = objDataSet;
                rptManageCategory.DataBind(); 
            }
        }
        catch (Exception ex)
        {
            _logger.Error("mobile_Articles:::Mobile Version:::::GetMainArticlsNews:::" + ex.Message);
        }
    }

    /// <summary>
    /// return Date Format
    /// </summary>
    /// <param name="sDataTime">set datetime "string value"</param>
    /// <returns>return date just</returns>
    protected string GetDateFormat(string sDataTime)
    {
        try
        {
            return Convert.ToDateTime(sDataTime).ToShortDateString();
        }
        catch (Exception)
        {
            return "Unknown";
        }
    }

    /// <summary>
    /// check Vedio Flag
    /// </summary>
    /// <param name="sVedioFlag">set Vedio Flag "string value"</param>
    /// <returns></returns>
    protected string checkVedioFlag(string sVedioFlag)
    {
        try
        {
            if (sVedioFlag == "" || sVedioFlag == "null")
                return "none";
            else
                return "";
        }
        catch (Exception)
        {
            return "none";
        }
    }

    /// <summary>
    /// RunVedio
    /// </summary>
    /// <param name="VedioURL">set Vedio URL "string value"</param>
    /// <returns></returns>
    protected string RunVedio(string VedioURL)
    {
        try
        {
            return "https://www.youtube.com/embed/" + GetThumnb(VedioURL) + "?feature=player_detailpage";
        }
        catch (Exception)
        {

            throw;
        }
    }

    /// <summary>
    /// GetThumnb
    /// </summary>
    /// <param name="sYoutubeURL">set Youtube URL "string  value"</param>
    /// <returns></returns>
    protected string GetThumnb(string sYoutubeURL)
    {
        try
        {
            string[] parts = sYoutubeURL.Split(new[] { "=" }, int.MaxValue, StringSplitOptions.None);
            return parts[1];
        }
        catch (Exception ex)
        {
            _logger.Error("mobile_Articles:::Mobile Version:::::GetThumnb:::" + ex.Message);
            return "null";
        }
    }

    /// <summary>
    /// GetThumManager
    /// </summary>
    /// <param name="sArticlsThumb"></param>
    /// <param name="sArticlsVedio"></param>
    /// <returns></returns>
    protected string GetThumManager(string sArticlsThumb, string sArticlsVedio)
    {
        try
        {
            if (sArticlsVedio == "null" || string.IsNullOrEmpty(sArticlsVedio))
            {
                return "../../../" + sArticlsThumb;
            }
            else
            {
                string[] parts = sArticlsVedio.Split(new[] { "=" }, int.MaxValue, StringSplitOptions.None);
                return "http://img.youtube.com/vi/" + parts[1] + "/mqdefault.jpg";
            }
        }
        catch (Exception ex)
        {
            _logger.Error("mobile_Articles:::Mobile Version:::::GetThumManager:::" + ex.Message);
            return "";
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sMetaTagType"></param>
    /// <returns></returns>
    protected string FacebookMetaTagHelper(string sMetaTagType)
    {
        try
        {
            ManageArticls objManageArticls = new ManageArticls();
            objArticls = objManageArticls.GetArticlsInfo(hfArticleID.Value);
            switch (sMetaTagType)
            {
                case "0":
                    if (objArticls.ArticlsImage == "null")
                    {
                        return GetThumManager("", objArticls.ArticlsVedio);
                    }
                    else
                    {
                        return objArticls.ArticlsImage.Replace("../", "http://livekora.net/");
                    }
                case "1":
                    return objArticls.ArticlsTitle + " | LiveKora لايف كورة ";
                case "2":
                    if (objArticls.ArticlsSmallDescription.Length > 26)
                        return objArticls.ArticlsSmallDescription.Replace("<p>", "").Replace("</p>", "");
                    else
                        return objArticls.ArticlsSmallDescription.Remove(0, 27);
                default:
                    return "null";
            }
        }
        catch (Exception ex)
        {
            _logger.Error("mobile_Articles:::Mobile Version:::::FacebookMetaTagHelper:::" + ex.Message);
            return "null";
        }
    }

    /// <summary>
    /// Generate URL
    /// </summary>
    /// <param name="Title"></param>
    /// <param name="strId"></param>
    /// <returns></returns>
    protected string GenerateURL(object Title, object strId)
    {
        try
        {
            string strTitle = Title.ToString();
            hfCurrentURL.Value = string.Empty;
            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');
            strTitle = strTitle.ToLower();
            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
            strTitle = strTitle.Replace("c#", "C-Sharp");
            strTitle = strTitle.Replace("vb.net", "VB-Net");
            strTitle = strTitle.Replace("asp.net", "Asp-Net");
            strTitle = strTitle.Replace(".", "-");
            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }
            strTitle = strTitle.Replace(" ", "-");
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("-----", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');
            strTitle = "../../../mobile/Articles/" + strId + "/" + strTitle;
            hfCurrentURL.Value = "http://livekora.net/mobile/" + strTitle.Replace("/../../", "/").Replace("../", "");
            return strTitle;
        }
        catch (Exception)
        {

            return "";
        }
    }

    protected string GenerateURLFacebookLike(object Title, object strId)
    {
        try
        {
            string strTitle = Title.ToString();
            hfCurrentURL.Value = string.Empty;
            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');
            strTitle = strTitle.ToLower();
            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
            strTitle = strTitle.Replace("c#", "C-Sharp");
            strTitle = strTitle.Replace("vb.net", "VB-Net");
            strTitle = strTitle.Replace("asp.net", "Asp-Net");
            strTitle = strTitle.Replace(".", "-");
            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }
            strTitle = strTitle.Replace(" ", "-");
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("-----", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Trim();
            strTitle = strTitle.Trim('-');
            strTitle = "../../../mobile/Articles/" + strId + "/" + strTitle;
            hfCurrentURLLike.Value = "http://livekora.net/mobile/Articles/" + strId + "/";
            return strTitle;
        }
        catch (Exception)
        {
            return "";
        }
    }

    #endregion
}